package com.linkage.linkedlife.ui.activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.ViewFlipper
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.clark.dingding.constants.ARouterPath
import com.linkage.linkedlife.R
import com.linkage.linkedlife.base.BaseActivity
import com.linkage.linkedlife.databinding.ActivityGuidanceBinding


/**
 * @ClassName: GuidanceActivity
 * @Description: java类作用描述
 * @Author: Clark
 * @Date: 2021/1/13 0013 8:52
 */
@Route(path = ARouterPath.GuidanceActivity)
class GuidanceActivity : BaseActivity(), View.OnClickListener {
    private lateinit var dataBinding: ActivityGuidanceBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_guidance)
        dataBinding.guideFliper.clearFocus()
        mContext = this
        var imgNum = 0
        while (imgNum < 4) {
            var view = LayoutInflater.from(this).inflate(R.layout.item_guide, null)
            var guideImage: ImageView = view.findViewById(R.id.guide_item_img)
            var guideText: TextView = view.findViewById(R.id.guide_item_text)
            guideImage.setImageDrawable(resources.getDrawable(R.drawable.ic_home_black_24dp))
            guideText.text = "这是第" + imgNum + "页"
            dataBinding.guideFliper.addView(view)
            imgNum++
        }
        guideFliper = dataBinding.guideFliper
        guideButton = dataBinding.guideStart
        dataBinding.guideFliper.setInAnimation(this, R.anim.anim_right_in);
        dataBinding.guideFliper.setOutAnimation(this, R.anim.anim_left_out);
        dataBinding.guideFliper.setFlipInterval(2000);
        dataBinding.guideFliper.setOnScrollChangeListener(View.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            Log.d(
                "clark",
                v.tag.toString() + scrollX + " " + scrollY + " " + oldScrollX + " " + scrollY
            )
        })

        // 1、设置幻灯片的形式滚动
        // viewFlipper.startFlipping();

        // 2、设置自动翻页滚动
        dataBinding.guideFliper.setAutoStart(false);
        dataBinding.guideFliper.isAutoStart();
        guideListener = MyGestureListener()
        mDetector = GestureDetector(this, guideListener)
        guideButton.setOnClickListener(this)


    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return mDetector.onTouchEvent(event)
    }


    private class MyGestureListener : GestureDetector.SimpleOnGestureListener() {
        override fun onFling(e1: MotionEvent, e2: MotionEvent, v: Float, v1: Float): Boolean {
            Log.d("clark", "" + guideFliper.displayedChild)
            if (e1.x - e2.x > MIN_MOVE) {
                if (guideFliper.displayedChild == 3) {
                    guideFliper.stopFlipping()
                    return false
                }
                guideFliper.setInAnimation(mContext, R.anim.anim_right_in)
                guideFliper.setOutAnimation(mContext, R.anim.anim_right_out)
                guideFliper.showNext()
                if (guideFliper.displayedChild == 3) {
                    guideButton.visibility = View.VISIBLE
                }
            } else if (e2.x - e1.x > MIN_MOVE) {
                if (guideFliper.displayedChild == 0) {
                    guideFliper.stopFlipping()
                    return false
                }
                guideFliper.setInAnimation(mContext, R.anim.anim_left_in)
                guideFliper.setOutAnimation(mContext, R.anim.anim_left_out)
                guideFliper.showPrevious()
            }
            return true
        }
    }

    companion object {
        var MIN_MOVE = 200 //最小距离
        lateinit var mContext: Context
        lateinit var guideFliper: ViewFlipper
        lateinit var guideButton: Button
        lateinit var guideListener: GestureDetector.SimpleOnGestureListener
        lateinit var mDetector: GestureDetector
    }

    override fun onClick(v: View) {
        if (v.id == R.id.guide_start) {
            ARouter.getInstance().build(ARouterPath.MainActivity).navigation()
        }
    }


}